﻿/*
* 注册审核
* 
*/
var TaskAudit = angular.module("TaskAuditModel", ['tm.pagination', 'btnconqitk', 'knowledgeSelector', 'knowledgebaseS', 'Common', "loadModule"])
.controller("TaskAuditController", ["$scope", "$rootScope", "$stateParams", "sysUserInfo", "RequestTrainService", "Comm", "Msg", "MsgCon", function ($scope, $rootScope, $stateParams, sysUserInfo, RTS, Comm, Msg, MsgCon) {
    $scope.btnlist = { examineinfo: false, delinfo: false };
    $rootScope.btnTab = "index.contentAudit.TaskAudit";
    //初始化
    $scope.init = function () {
        //培训任务列表集合
        $scope.List = [];
        // 选中个数
        $scope.checkAll = 0;
        //查询培训任务列表参数
        $scope.params = {
            userid: sysUserInfo.getuserinfo().user_ID,  //登录人id
            orgId: sysUserInfo.getuserinfo().organization_ID, //登录人企业id
            reviewed: "", //审核状态 1.审核中 2.审核成功 3.审核失败
            xid: "", //任务id
            xname: "", //任务名称
            startDate: "",  //开始时间
            endDate: "",    //结束时间
            searchText: "",    //搜索内容、（知识结构名称或拼音）
            pageIndex: 1,   //起始下标
            pageSize: 10 //页数
        }
        $scope.getList();

    }

    //日期筛选
    $scope.GetDate = function () {
        $scope.params.startDate = document.getElementById("startDate").value;
        $scope.params.endDate = document.getElementById("endDate").value;
    }
    //全选
    $scope.selectAll = function () {
        $scope.selFlag = true;
        if ($scope.checkAll == $scope.List.length) { //已选
            $scope.selFlag = false;
            $scope.checkAll = 0;
        } else {
            $scope.selFlag = true;
            $scope.checkAll = $scope.List.length;
        }
        // 设置样式
        angular.forEach($scope.List, function (data, index, array) {
            data.isSel = $scope.selFlag;
        });
    }
    // 单选
    $scope.singleCheck = function (item) {
        if (item.isSel) {
            $scope.checkAll--;
            item.isSel = false;
        } else {
            $scope.checkAll++;
            item.isSel = true;
        }
    }
    /*
    *   遍历选中的对象
    *   list 集合
    *   serIds ref  id集合
    *   serItems ref obj集合不用传null
    */
    $scope.getParamsList = function (list, serIds, serItems) {
        angular.forEach(list, function (data, ind, arr) {//循环全部对象
            if (data.isSel) {// 找到选中的对象
                serIds.push(data.id); // 存id
                if (serItems != null)
                    serItems.push(data); // 存obj
            }
        });
    }
    $scope.selName = "全部";
    //loading 加载
    $scope.Loading = false;
    //表头
    $scope.tabHeader = [];
    //获取列表
    $scope.getList = function () {

        $scope.loading = true;
        RTS.findEnrollPage($scope.params).then(function (response) {
            $scope.loading = false;
            if (response.errorcode == "0") {
                $scope.List = response.datas;
                $scope.checkall = 0; // 复选框选中的数量  
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
            } else {
                Msg.error("获取列表失败了呦！");
            }
        }, function errorcallback() {
            console.log("连接服务器失败！");
        });

    }
    //回车事件
    $scope.findListEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.getList();
        }
    }

    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.getList();
            }
            $scope.checkAll = 0; // 复选框选中的数量     
        }
    };

    //初始审核的参数
    $scope.initSH = function () {
        $scope.upItem = {
            type: 1,
            userid: sysUserInfo.getuserinfo().user_ID,
            userName: "",
            reviewed: "2",
            eid: "",
            remarks: ""
        };
    }
    $scope.initSH();
    //审核框
    $scope.ExaminerUser = function (item) {
        //打开
        if (!$scope.showExaminerUser) {
            var list = [];
            if (item == undefined) {
                $scope.List.forEach(function (data, index) {
                    if (data.isSel) {
                        list.push(data);
                    }
                });
                if (list.length <= 0) {
                    Msg.warning("请选择至少选择一条数据！");
                    return;
                } else if (list.length > 1) {
                    list.forEach(function (data, index) {
                        $scope.upItem.userName += ($scope.upItem.userName.length > 0 ? '、' : '') + data.userName
                    });
                    $scope.upItem.enrollTime = "";
                } else {
                    $scope.upItem.userName = list[0].userName;
                    $scope.upItem.enrollTime = list[0].enrollTime;
                }
            } else {
                list.push(item);
                $scope.upItem.userName = item.userName;
                $scope.upItem.enrollTime = item.enrollTime;
            }

            //批量审核
            if (list.length > 1) {
                var ids = [];
                var idlist = [];
                //遍历是否审核，是否为同一个班级
                list.forEach(function (data, index) {
                    if (data.reviewed == 1) {
                        ids.push(data);
                        idlist.push(data.enrollId);
                    } else {
                        data.isSel = false;
                    }
                });
                if (ids.length <= 0) {
                    $scope.checkAll = 0;
                    Msg.warning("您选中的数据无需审核，请不要重复操作！");
                    return;
                }
                if (ids.length < list.length) {
                    $scope.checkAll = ids.length;
                    Msg.warning("您选中了已审核的数据，已为您自动取消！");
                }
                $scope.upItem.eid = idlist.join(",");
            } else {
                if (list[0].reviewed != 1) {
                    Msg.warning("该学员已审核，请不要重复操作！");
                    return;
                }
                $scope.upItem.eid = list[0].enrollId;
            }

            $scope.showExaminerUser = !$scope.showExaminerUser;
        } else {
            $scope.showExaminerUser = !$scope.showExaminerUser;
        }

    }

    // 多个审核
    $scope.shInfo = function (list) {

        $scope.islivebtnload = true;
        // 发送请求
        RTS.modifyExamine($scope.upItem).then(function (respose) {
            $scope.islivebtnload = false;
            if (respose.errorcode == "0") {
                Msg.success("审核成功！");
                $scope.checkall = 0; // 复选框选中的数量            
                $scope.getList();  // 重新请求一下数据
                $scope.showExaminerUser = false;
                $scope.initSH();
            } else {
                Msg.error("操作失败！");
            }
        }, function errorcallback() {
            $scope.islivebtnload = false;
            console.log("连接服务器失败！");
        });


    }
    //***************************************************************我的选择器们*************************************************************************/

    //    配置知识选择器
    $scope.knowConfig = {   // 配置知识选择器的参数
        menu: ["trainingTask"],
        ksrMore: false, //班级 是否多选
        ksrList: [],    // 班级选中列表                 
        isShow: false  // 显示知识选择器
    };
    // 打开知识选择器
    $scope.opknowConfig = function (secinfo) {
        $scope.knowConfig.isShow = true;
    }
    //选择器回调
    $scope.callBack = function () {
        if ($scope.params.xid == $scope.knowConfig.ksrList[0].id) {
            $scope.params.xid = "";
            $scope.params.xname = "";
        } else {
            $scope.params.xid = $scope.knowConfig.ksrList[0].id;
            $scope.params.xname = $scope.knowConfig.ksrList[0].name;
        }
        $scope.getList();
    }
    $scope.init();

    //删除人员
    $scope.delinfo = function () {
        $scope.Loading = true;
        var id2 = [];
        $scope.List.forEach(function (data, index) {
            if (data.isSel) {
                id2.push(data.enrollId);
            }
        });
        if (id2.length <= 0) {
            Msg.warning("请至少选择一条数据！");
            return;
        }
        MsgCon.delnoicon("提示", "确定直接删除" + id2.length + "条数据？", "", "确定", "取消").then(function (flag) {   // 删除弹框
            if (flag) {
                $scope.islivebtnload = true;
                // 发送请求
                RTS.modifyExamine({ type: 2, eid: id2.join(","), userid: sysUserInfo.getuserinfo().user_ID }).then(function (respose) {
                    $scope.Loading = false;
                    if (respose.errorcode == "0") {
                        Msg.success("删除成功！");
                        $scope.checkall = 0; // 复选框选中的数量            
                        $scope.getList();  // 重新请求一下数据
                    } else {
                        Msg.error("删除失败！");
                    }
                }, function errorcallback() {
                    $scope.Loading = false;
                    console.log("连接服务器失败！");
                });
            }
        })
    }
} ]);
